仔細看了看昨天信件剩餘的題目,
hmm.. 我們還是換題好了 σ`∀´)σ
Write a program that will calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * ... * N
zeros(6) = 1
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero
zeros(12) = 2
# 12! = 479001600 --> 2 trailing zeros
坦白說第一想法是:爆開(X
但爆開只會通往 TLE 的道路。
所以我們還是乖乖的求救 Google 大神 qwq
尾數有 0 的情況為 五的倍數 * 二的倍數所形成的,
雖然可以找這個數字有幾個 2 及 幾個 5,
但 2 的倍數太多了,
所以決定性的因素就在 5 身上。
def zeros(n):
count = 0
while n >= 5:
count += n//5
n //= 5
return count
我這個笨蛋想了頗久,
為甚麼可以一直取 5 的商數,
後來才理解可以整理為這個公式:
count = n // 5 + n // 25 + n // 125 + ...
The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
"din" => "((("
"recede" => "()()()"
"Success" => ")())())"
"(( @" => "))(("
這題對於 python 來說很簡單,
有重複 => ")"
沒重複 => "("
另外記得全部統一大小寫RRR
(本人犯的低級錯誤)
def duplicate_encode(word):
word = word.lower()
ans = ""
for i in word:
if word.count(i) > 1:
ans += ")"
else: ans += "("
return ans
題外話,
今天好不容易把 AD 驗證的密碼到期日判定寫完,
其實頗有成就感的啦 XD
果然睡了一覺起床就解決了 (X
離開學剩下三天,
希望不要再晚睡了RRRR qwq